package form.ui.component.Select {
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import form.events.OptionEvent;
	import form.ui.core.Button;

	public class Option extends Button {
		public static const WIDTH:int = 10000;
		public static const HEIGHT:int = 18;
		
		public static const FONT_COLOR:int = 0x8f8f8f;
		
		public static const HOVER_FILL_COLOR:int = 0xffdcb6;
		public static const HOVER_FONT_COLOR:int = 0x8f8f8f;
		
		public static const SELECTED_FILL_COLOR:int = 0xe1eaf3;
		
		public static const FONT_FAMILY:String = "Arial";
		public static const FONT_SIZE:int = 10;
		
		public static const TEXT_INDENT:int = 10;
		
		private var _value:String;
		private var _text:String;
		
		protected var mouseUpTextField:TextField = new TextField();
		protected var mouseOverTextField:TextField = new TextField();
		protected var mouseDownTextField:TextField = new TextField();
		protected var selectedTextField:TextField = new TextField();
		
		public function Option() {
			super();
			
			Option.defineMouseUpTextField(this.mouseUpTextField);
			Option.defineMouseOverAndDownTextField(this.mouseOverTextField);
			Option.defineMouseOverAndDownTextField(this.mouseDownTextField);
			Option.defineSelectedTextField(this.selectedTextField);
			
			this.mouseUp = this.mouseUpTextField;
			this.mouseOver = this.mouseOverTextField;
			this.mouseDown = this.mouseDownTextField;
			this.disabled = this.selectedTextField;
			
			this.enable();
		}
		
		public function set text(value:String):void {
			this._text = value;
			
			this.mouseUpTextField.text = this.text;
			this.mouseOverTextField.text = this.text;
			this.mouseDownTextField.text = this.text;
			this.selectedTextField.text = this.text;
		}
		
		public function get text():String {
			return this._text;
		}
		
		public function get value():String {
			return this._value;
		}
		
		public function set value(string:String):void {
			this._value = string;
		}
		
		public override function mouseUpAction():void {
			this.select();
		}
		
		public function show():void {
			var e:OptionEvent = new OptionEvent(OptionEvent.SHOW);
			e.value = this.value;
			e.text = this.text;
			e.option = this;
			
			this.dispatchEvent(e);
		}
		
		public function select():void {
			var e:OptionEvent = new OptionEvent(OptionEvent.SELECTED);
			e.value = this.value;
			e.text = this.text;
			e.option = this;
			
			this.dispatchEvent(e);
		}
		
		protected static function defineMouseUpTextField(textField:TextField):void {
			textField.width = Option.WIDTH;
			textField.height = Option.HEIGHT;
			textField.defaultTextFormat = Option.textFormat;
			
			textField.textColor = Option.FONT_COLOR;
			textField.background = false;
		}
		
		protected static function defineMouseOverAndDownTextField(textField:TextField):void {
			textField.width = Option.WIDTH;
			textField.height = Option.HEIGHT;
			textField.defaultTextFormat = Option.textFormat;
			
			textField.textColor = Option.HOVER_FONT_COLOR;
			textField.backgroundColor = Option.HOVER_FILL_COLOR;
			textField.background = true;
		}
		
		protected static function defineSelectedTextField(textField:TextField):void {
			textField.width = Option.WIDTH;
			textField.height = Option.HEIGHT;
			textField.defaultTextFormat = Option.textFormat;
			
			textField.textColor = Option.FONT_COLOR;
			textField.backgroundColor = Option.SELECTED_FILL_COLOR;
			textField.background = true;
		}
		
		protected static function get textFormat():TextFormat {
			var format:TextFormat = new TextFormat();
			
			format.font = Option.FONT_FAMILY;
			format.size = Option.FONT_SIZE;
			format.indent = Option.TEXT_INDENT;
			
			return format;
		}
		
		protected override function onAddedToStage(e:Event):void {}
	}
}